package edu.jmu.proxyjdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;;

public class MyBeanFactoryJDK {
	public static UserDao getBean() {
		//1.准备目标类
		final UserDao userDao = new UserDaoImpl();
		//2.创建切面实现类
		final MyAspect myAspect = new MyAspect();
		//3.使用代理类，进行增强
		InvocationHandler handler = new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				myAspect.myBefore();
				Object object = method.invoke(userDao,args);
				myAspect.myAfter();
				return object;
			}
		};
		
		UserDao newProxyInstance = (UserDao)Proxy.newProxyInstance(
				MyBeanFactoryJDK.class.getClassLoader(), 
				new Class[]{UserDao.class}, 
				handler);
		
		return newProxyInstance;
	}
}
